/* 
*** Heterogeneity analysis to explore potential mechanisms
	// input: inclevel_dataset, union_category, union_boards_panel_20112019
	// output: Table4.xlsx (sheet: t4_panelB)
*/ 

cap log close
cap log using "$logs/inclevel_heterogx_log", replace

* data toggles
local singest = 0 //0: no restriction; 1: worker's baseline establishment must be a single establishment firm
local bothFM = 1 //0: no restriction; 1: worker's baseline establishment must employ both men and women at basline
local covered = 1 //0: no restriction; 1: worker's baseline establishment must be in the geographic coverage of the CBA
local signing = 1 //0: no restriction; 1: worker's baseline establishment must be a signing establishment
local geoglevel = "microregion_mode" //"": for no geog-yr FES; "state_mode": for state-yr FEs; "microregion_mode": for microregion-yr FEs
local no2011 = 1 //0: include 2011; 1: exclude 2011
local restr = 0 //0: no restriction; 1: worker must be 18 or older and not be a in probation at baseline

* load incumbent level data
use "$files/inclevel_dataset.dta", clear

* sample restrictions
if `singest' {
	keep if singestwork==1
}
if `bothFM' {
	keep if bothFMwork==1
}
if `covered' {
	keep if coverwork==1
}
if `signing' {
	keep if signwork==1
}
if `restr' {
	keep if blage>=18
	gen prob=(blten<=3)
	drop if prob==1
}

* time varying fixed effects
//industry
gen ind2d_num = floor(blind/1e3)
gen ind3d_num = floor(blind/1e2)
egen indyrFE = group(year ind2d_num)
//geography
rename blstate state_mode
rename blmr microregion_mode
egen geogyrFE = group(year `geoglevel')
//tenure
gen ten3m = floor(blten/3)
egen tenyrFE = group(year ten3m)

* for clustering
gen clustergrp = blempl

* event study var 
gen post = (year>=2015) if year!=.
gen treatpost = treatwork*post
gen postgender = post*gender
gen treatgender = treatwork*gender 

tab year, gen(yearj)
	
forvalues i = 1/7 {
	gen interj`i'=treatwork*yearj`i'
}

lab var interj1 "2011"
lab var interj2 "2012"
lab var interj3 "2013"
lab var interj4 "2014"
lab var interj5 "2015"
lab var interj6 "2016"
lab var interj7 "2017"

gen zero=0
lab var zero "2014"
replace interj4 = zero

* Inverse of employment: for weighting regressions
gen blfeminv=1/blfem
gen blmalinv=1/blmal

gen blinv=blfeminv if gender==1
replace blinv=blmalinv if gender==0

* Outcome variables
label var atblemp	"At baseline employer"

* Heterogeneity
gen xxx = union_id if year==2014
egen yyy = mode(xxx), by(fakeid_worker) minmode
replace union_id = yyy
drop xxx yyy

merge m:1 union_id using "$raw\CNES\union_category"
drop if _merge==2
drop _merge

preserve
	use "$files/union_boards_panel_20112019.dta", clear
	keep if year>=2012 & year<=2017
	gen xxx = sh_female if year==2014
	gen yyy = sh_female if year>2014
	egen xxxm = max(xxx), by(union_id)
	egen yyym = max(yyy), by(union_id)
	gen gain_sh_female = (xxxm<yyym)
	drop xxx* yyy*
	gen xxx = sh_female_exec if year==2014
	gen yyy = sh_female_exec if year>2014
	egen xxxm = max(xxx), by(union_id)
	egen yyym = max(yyy), by(union_id)
	gen gain_sh_female_exec = (xxxm<yyym)
	drop xxx* yyy*
	gen xxx = sh_female_execd if year==2014
	gen yyy = sh_female_execd if year>2014
	egen xxxm = max(xxx), by(union_id)
	egen yyym = max(yyy), by(union_id)
	gen gain_sh_female_execd = (xxxm<yyym)
	drop xxx* yyy*
	keep union_id gain_*
	duplicates drop
	tempfile temp
	save `temp'
restore
merge m:1 union_id using `temp'
drop if _merge==2
drop _merge

gen d1_het = 1
replace d1_het = 0 if ///
	(c_group=="servidores_publicos") | ///
	(c_group=="rural") | ///
	(c_subgroup=="ensino") | ///
	(c_subgroup=="fia_tec") | ///
	(c_group=="emp_credito") | ///
	(c_subgroup=="saude") | ///
	(c_subgroup=="comunica") | ///
	(c_subgroup=="prof_lib")

gen d2_het = 1
replace d2_het = 0 if ///
	(c_subgroup=="metalurgica") | ///
	(c_subgroup=="saude") | ///
	(c_subgroup=="atacadista"|c_subgroup=="varejista"|c_subgroup=="autonomo") | ///
	(c_subgroup=="comunica") 

gen d3_het = 1
replace d3_het = 0 if ///
	(microregion_mode==35061)| ///
	(microregion_mode==26017)| ///
	(microregion_mode==42016)| ///
	(microregion_mode==52010)| ///
	(microregion_mode==31030)| ///
	(microregion_mode==13007) 
	
gen d4_het = 1
replace d4_het = 0 if ///
	(gain_sh_female_execd==1)

* Regressions
if `no2011' {
	drop if year==2011
	drop interj1
	local xl = 3.5
}
else {
	local xl = 4.5
}

//female; all ages
* main results
local i = 0
foreach var of varlist atblemp {	
		local i = `i'+1 
		reghdfe `var' treatpost if gender==1 [aweight=blfeminv], absorb(fakeid_worker *FE) cluster(clustergrp)
		su `var' if gender==1 & treatwork == 1 & year==2014
		estadd scalar meandv=`r(mean)'
		estadd scalar obs=e(N_full)
		estimates store al`i'
	}			
* interact with industries gaining female representative in CUT (union info)
gen dummy = (d1_het==0) if d1_het!=.
local i = 0
foreach var of varlist atblemp {	
		local i = `i'+1 
		reghdfe `var' treatpost##dummy treatwork##dummy post##dummy if gender==1 [aweight=blfeminv], absorb(fakeid_worker *FE) cluster(clustergrp)
		estadd scalar obs=e(N_full)
		qui lincom 1.treatpost + 1.treatpost#1.dummy
		estadd scalar sumdum =r(estimate)
		qui test 1.treatpost + 1.treatpost#1.dummy = 0
		estadd scalar sumdump =r(p)
		qui su `var' if gender==1 & year == 2014 & treatwork == 1
		estadd scalar meandv=r(mean)
		estimates store bl`i'
}			
drop dummy
* interact with industries proposing fight plan (union info)
gen dummy = (d2_het==0) if d2_het!=.
local i = 0
foreach var of varlist atblemp {	
		local i = `i'+1 
		reghdfe `var' treatpost##dummy treatwork##dummy post##dummy if gender==1 [aweight=blfeminv], absorb(fakeid_worker *FE) cluster(clustergrp)
		estadd scalar obs=e(N_full)
		qui lincom 1.treatpost + 1.treatpost#1.dummy
		estadd scalar sumdum =r(estimate)
		qui test 1.treatpost + 1.treatpost#1.dummy = 0
		estadd scalar sumdump =r(p)
		qui su `var' if gender==1 & year == 2014 & treatwork == 1
		estadd scalar meandv=r(mean)
		estimates store cl`i'
}			
drop dummy
* interact with microregions with CUT schools
gen dummy = (d3_het==0) if d3_het!=.
local i = 0
foreach var of varlist atblemp {	
		local i = `i'+1 
		reghdfe `var' treatpost##dummy treatwork##dummy post##dummy if gender==1 [aweight=blfeminv], absorb(fakeid_worker *FE) cluster(clustergrp)
		estadd scalar obs=e(N_full)
		qui lincom 1.treatpost + 1.treatpost#1.dummy
		estadd scalar sumdum =r(estimate)
		qui test 1.treatpost + 1.treatpost#1.dummy = 0
		estadd scalar sumdump =r(p)
		qui su `var' if gender==1 & year == 2014 & treatwork == 1
		estadd scalar meandv=r(mean)
		estimates store dl`i'
}			
drop dummy

* interact gaining leader
gen dummy = (d4_het==0) if d4_het!=.
gen treatdummy = treatwork*dummy
gen treatpostdummy = treatpost*dummy
local i = 0
foreach var of varlist atblemp {	
		local i = `i'+1 
		reghdfe `var' treatwork post treatpost treatdummy treatpostdummy if gender==1 [aweight=blfeminv], absorb(fakeid_worker *FE) cluster(clustergrp)
		estadd scalar obs=e(N_full)
		qui lincom treatpost + treatpostdummy
		estadd scalar sumdum =r(estimate)
		qui test treatpost + treatpostdummy = 0
		estadd scalar sumdump =r(p)
		qui su `var' if gender==1 & year == 2014 & treatwork == 1
		estadd scalar meandv=r(mean)
		estimates store el`i'
}			
drop dummy treatdummy treatpostdummy

*** Export ***
preserve
forvalues j = 1/1 {
		#d ;
		estout al`j' bl`j' cl`j' dl`j' el`j' using "$tables/T4_xhet_incfemall`j'.txt", 
		style(tab) mlabels(none) label collabels(none)
		cells(b(star fmt(%9.3f)) se(par)) 
		stats(sumdum sumdump meandv obs, fmt(%9.3f %9.3f %9.2fc %9.0fc ) labels("sum" "p-value" "mean depvar" "N" )) 
		drop(o.*, relax) keep(treatpost 1.treatpost 1.treatpost#1.dummy treatpostdummy, relax) replace starlevels(* 0.10 ** 0.05 *** 0.01);
		#d cr
	}	

local lab1 atblemp

local r1 = 1

import delimited "$tables/T4_xhet_incfemall1.txt", clear
rename v1 `lab1'
rename v2 main
rename v3 new_reps
rename v4 conf_prop
rename v5 cut_school
rename v6 fem_leader_int
		
export excel using "$tables/Table4.xlsx", sheet("t4_panelB") cell(A`r1') firstrow(var) sheetreplace

restore

cap log close
